/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.deib.se2.meteocal.business.boundary;

import it.polimi.deib.se2.meteocal.business.boundary.CalendarManager;
import it.polimi.deib.se2.meteocal.business.entity.Calendar;
import javax.ejb.EJB;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class CalendarManagerIT {
    
    @EJB
    CalendarManager cal;
    @PersistenceContext
    EntityManager em;
    
    @Deployment
    public static WebArchive createArchiveAndDeploy() {
        return ShrinkWrap.create(WebArchive.class)
                .addClass(CalendarManager.class)
                .addPackage(Calendar.class.getPackage())
                .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
    }
    
    @Test
    public void CalendarManagerShouldBeInjected() {
        assertNotNull(cal);
    }
    
    @Test
    public void EntityManagerShouldBeInjected() {
        assertNotNull(em);
    }
    
    @Test
    public void newCalendarsShouldBelongToUser() {
        Calendar newCalendar = new Calendar();
        try{
            newCalendar.getOwner();
        }catch(NullPointerException e ){
            assertTrue(true);
        }
    } 
    
     @Test
    public void newCalendarShouldHaveId() {
        Calendar newCalendar = new Calendar();
        newCalendar.setId(null);
        try {
            cal.save(newCalendar);
        } catch (Exception e) {
            assertTrue(true);
        }
    }
    
    @Test
    public void noSameIdForTwoCalendars() {
        Calendar c1 = new Calendar();
        Calendar c2 = new Calendar();
        c1.setId(Long.MIN_VALUE);
        c2.setId(Long.MIN_VALUE);
        try {
            cal.save(c1);
            cal.save(c2);
        } catch(Exception e) {
            assertTrue(true);
        }
    }
        
}
